package com;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import lombok.extern.slf4j.Slf4j;

/**
 * 引导服务器
 */
@Slf4j
public class MonitorServer {

    public static final int PORT = 7777;

    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        ServerBootstrap bootstrap = new ServerBootstrap();
        bootstrap.group(group, workerGroup).channel(NioServerSocketChannel.class)
                .childHandler(new MonitorServerInitializer());

        Channel channel = bootstrap.bind(PORT).sync().channel();
        TransferRtspHelper.schedule();
        log.info("服务器开始运行......");

        channel.closeFuture().sync();

        Runtime.getRuntime().addShutdownHook(new Thread(() -> {
            group.shutdownGracefully();
            workerGroup.shutdownGracefully();
            ChannelGroupHolder.close();
            log.info("服务器已关闭");
        }));
    }

}
